package com.bds.tjj.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bds.tjj.model.News;
import com.bds.tjj.model.common.Page;
import com.bds.tjj.model.common.Result;
import com.bds.tjj.service.NewsService;
import com.bds.tjj.utils.Const;

@Controller
public class NewsController extends BaseAction {
	private org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(NewsController.class);
	@Resource
	NewsService newsService;

	// 返回新闻模板
	@RequestMapping("/analytics/news")
	public String news(Map<String, Object> map, HttpServletRequest request,String v) {
		if (StringUtils.isEmpty(v)) {
			return "ftl1/analytics/news";
		} else if (v.equals("1")) {
			return "ftl/analytics/news";
		} else {
			return "ftl1/analytics/news";
		}
		
	}

	@RequestMapping(value = "/data/news/getNewsList")
	@ResponseBody
	public Page getMessagePage(HttpServletRequest request, HttpServletResponse response, Integer pageNum,
			Integer pageSize, Integer isread) throws Exception {
		Page page = new Page();
		try {
			Long user_id = this.getUserSession(request).getUser_id();

			// 更改为已读过

			page = newsService.getNewsList(user_id, pageNum, pageSize, true);
		} catch (Exception e) {
			page.setCode(Const.FAIL);
			LOGGER.error("", e);
		}
		return page;
	}

	@RequestMapping("/data/news/getMessagesNews")
	@ResponseBody
	public Result getMessagesNews(HttpServletRequest request) {
		Result ret = new Result();
		try {
			Long userid = this.getUserSession(request).getUser_id();
			List<News> messages = newsService.getMessagesNews(userid, 10);
			ret.setData(messages);
			ret.setCode(Const.SUC);
		} catch (Exception e) {
			ret.setCode(Const.FAIL);
			ret.setMsg("查询最新消息错误");
		}

		return ret;

	}

	@RequestMapping("/data/news/detail/{newsid}")
	public String toNewsLink(HttpServletRequest request, @PathVariable Long newsid) {

		try {
			Long userid = this.getUserSession(request).getUser_id();
			News n = newsService.upNews(userid, newsid);
			return "redirect:"+n.getLink();
		} catch (Exception e) {
			LOGGER.error("", e);
			return "/";
		}

	}
}
